package cn.car91.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import cn.car91.R;
import cn.car91.bean.Comment;
import cn.car91.bean.RepairShopComment;

public class CommentListAdapter<T> extends ArrayAdapter {
    private int resource;
    private LayoutInflater layoutInflater;
    private List<T> data;

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public void addData(List<T> data) {
        this.data.addAll(data);
    }

    public CommentListAdapter(Context context, int resource) {
        super(context, resource);
        // TODO Auto-generated constructor stub
        this.resource = resource;
        this.layoutInflater = LayoutInflater.from(context);
        this.data = new ArrayList<T>();
    }

    public final class CommentItem {
        public TextView user;
        public TextView content;
        //public TextView grade;
        public TextView time;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return data.get(arg0);
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        CommentItem commentItem = null;
        if (arg1 == null) {
            commentItem = new CommentItem();

            arg1 = layoutInflater.inflate(resource, null);
            commentItem.user = (TextView) arg1.findViewById(R.id.comment_user);
            //commentItem.grade = (TextView) arg1.findViewById(R.id.comment_grade);
            commentItem.content = (TextView) arg1.findViewById(R.id.comment_content);
            commentItem.time = (TextView) arg1.findViewById(R.id.comment_time);

            arg1.setTag(commentItem);
        } else {
            commentItem = (CommentItem) arg1.getTag();
        }

        T comment = (T) getItem(arg0);

        if(comment.getClass().equals(RepairShopComment.class)){
        	RepairShopComment repairShopComment = (RepairShopComment) comment;
        	commentItem.user.setText(repairShopComment.getUsername());
            commentItem.content.setText(repairShopComment.getContent());
            /*if(repairShopComment.getScore()!=null){
            	switch (repairShopComment.getScore()) {
      				case 0:
      		            commentItem.grade.setText("评价：好评");
      					break;
      				case 1:
      					commentItem.grade.setText("评价：中评");
      					break;
      				case 2:
      					commentItem.grade.setText("评价：差评");
      					break;
      				default:
      					break;
      				}
            }*/
            commentItem.time.setText(DateFormat.format("yy-MM-dd kk:mm", repairShopComment.getTime()));
        } else {
        	Comment comment2 = (Comment) comment;
	        commentItem.user.setText(comment2.getUsername());
	        commentItem.content.setText(comment2.getContent());
	
	        commentItem.time.setText(DateFormat.format("yy-MM-dd kk:mm", comment2.getTime()));
        }
        return arg1;
    }

}
